package com.aaa.car.controller;

import com.aaa.car.entity.Car;
import com.aaa.car.entity.MaintainRecord;
import com.aaa.car.service.CarService;
import com.aaa.car.service.MaintainRecordService;
import com.aaa.common.util.RandomUtil;
import com.aaa.common.util.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * (Car)表控制层
 *
 * @author YuXin
 * @since 2020-11-19 19:32:33
 */
@RestController
@RequestMapping("car")
public class CarController {
    /**
     * 服务对象
     */
    @Resource
    private CarService carService;

    @Resource
    private MaintainRecordService maintainRecordService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Car selectOne(@RequestParam("id") Integer id) {
        return this.carService.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param
     * @param
     * @return 对象列表
     */
    @PostMapping("queryAllCar")
    public Result queryAllCar(@RequestBody Map map) {
        Integer pageNo = (Integer) map.get("pageSize");
        Integer pageSize = (Integer) map.get("pageSize");
        return this.carService.queryAllCar(pageNo, pageSize, map);
    }

    /**
     * 查询多条数据
     *
     * @param
     * @param
     * @return 对象列表
     */
    @PostMapping("queryAll")
    public List<Map> queryAll(@RequestBody Map map) {
        return this.carService.queryAll(map);
    }

    /**
     * 新增数据
     *
     * @param car 实例对象
     * @return 实例对象
     */
    @PostMapping("insert")
    public Car insert(@RequestBody Car car) {
        String vin = RandomUtil.randomCode();
        car.setVin(vin);
        this.carService.insert(car);
        return car;
    }


    /**
     * 新增数据
     *
     * @param maintainRecord 实例对象
     * @return 实例对象
     */
    @PostMapping("carMaintainAdd")
    public MaintainRecord carMaintainAdd(@RequestBody MaintainRecord maintainRecord) {
        maintainRecord.setAddtime(new Date());
        this.maintainRecordService.insert(maintainRecord);
        return maintainRecord;
    }
    /**
     * 修改数据
     *
     * @param car 实例对象
     * @return 实例对象
     */
    @PutMapping("update")
    public Car update(@RequestBody Car car) {
        return this.carService.update(car);
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @DeleteMapping("deleteById")
    public boolean deleteById(Integer id) {
        return this.carService.deleteById(id);
    }
}